/*
 *
 * Copyright 2013 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.netflix.client;

import java.net.ConnectException;

/**
 * A handler that determines if an exception is retriable for load balancer,
 * and if an exception or error response should be treated as circuit related failures
 * so that the load balancer can avoid such server.
 *
 * 一种处理程序，用于确定负载平衡器是否可以重试异常，以及是否应将异常或错误响应视为与电路相关的故障，以便负载平衡器可以避免此类服务器故障
 *
 * 说人话就是，抛异常的时候，是重试还是继续抛出。
 *
 *
 *
 * 重试处理器之所以会放在 ribbon-core 中，而不是 ribbon-loadbalance 中，是因为重试其实
 * 属于执行过程中的概念，而 loadbalancer 则是执行前的概念，在执行前选出 server，然后执行请求，
 * 执行过程中若报错，则 retry 重试。
 * @author awang
 */
public interface RetryHandler {

    /**
     * 默认的重试处理器
     */
    public static final RetryHandler DEFAULT = new DefaultLoadBalancerRetryHandler();
    
    /**
     * Test if an exception is retriable for the load balancer
     * 测试此异常对负载均衡器来说是否可重试
     * 
     * @param e the original exception
     * @param sameServer if true, the method is trying to determine if retry can be 
     *        done on the same server. Otherwise, it is testing whether retry can be
     *        done on a different server
     *                   如果是 true。那么将会在同一个 server 上重试，否则找另一个 server 重试
     */
    public boolean isRetriableException(Throwable e, boolean sameServer);

    /**
     * Test if an exception should be treated as circuit failure.
     *
     *  是否是Circuit熔断类型异常。比如java.net.ConnectException就属于这种故障
     * 	这种异常类型一般属于比较严重的，发生的次数多了就会把它熔断（下次不会再找它了）
     * For example, a {@link ConnectException} is a circuit failure.
     *
     * This is used to determine
     * whether successive exceptions of such should trip the circuit breaker to a particular
     * host by the load balancer. If false but a server response is absent, 
     * load balancer will also close the circuit upon getting such exception.
     */
    public boolean isCircuitTrippingException(Throwable e);
        
    /**
     * @return Number of maximal retries to be done on one server
     * 同一个 server 的最大重试次数
     */
    public int getMaxRetriesOnSameServer();

    /**
     * @return Number of maximal different servers to retry
     * 要重试的最大不同服务器数，如果是2 表示最多去 2台 不同的服务器身上重试。
     *
     * 这里的重试就是因为捕获到的异常，若 isRetriableException 传的 sameServer 是 false，
     * 那么就会到别的服务器重试，该属性就是限制到多少台服务器上重试
     */
    public int getMaxRetriesOnNextServer();
}
